package com.br.negocio.web.backbean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.br.negocio.controller.ControllerCor;
import com.br.negocio.vo.CorVO;
import com.br.negocio.web.formbean.CorFormBean;

@Controller("corBackBean")
@Scope("session")
public class CorBackBean implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Resource
	private ControllerCor controllerCor;
	
	private CorFormBean corFormBean;

	public CorBackBean() {
		super();
		this.corFormBean = new CorFormBean();
	}
	public CorFormBean getCorFormBean() {
		return this.corFormBean;
	}

	public void setCorFormBean(CorFormBean corFormBean) {
		this.corFormBean = corFormBean;
	}
	
	// METODOS

	public void obterListaCor() {
		
		FacesContext context = FacesContext.getCurrentInstance();
		
		try {
			this.corFormBean.setListaCor(this.controllerCor.obterListaCor());
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("Erro ao obter a lista de cores: " + e.getMessage()));
		}
	}
	
	public void obterPdf() {
		
		FacesContext context = FacesContext.getCurrentInstance();

		try {
			@SuppressWarnings("rawtypes")
			Map parameterMap = new HashMap<String, String>();
			JasperReport jasperreport = JasperCompileManager.compileReport("ireport/consultaCor.jasper");  
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperreport, parameterMap);  
			JasperViewer.viewReport(jasperPrint,false);  
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("Erro ao obter a lista de cores: " + e.getMessage()));
		}
	}
	
	public void manterCor() {
		
		FacesContext context = FacesContext.getCurrentInstance();
		
		try {
			this.controllerCor.atualizarCor(this.corFormBean.getCorManter());
			this.corFormBean.setIsDisabled(true);
			context.addMessage(null, new FacesMessage("Sucesso: Inserido realizada com sucesso!"));
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("Erro ao manter cor: " + e.getMessage()));
		}

	}
	
	public void excluirCor() {
		
		FacesContext context = FacesContext.getCurrentInstance();
		
		try {
			if (this.getCorFormBean().getCorManter().getCodigo() != 0) {
				this.controllerCor.removerCor(this.corFormBean.getCorManter());
				if (!this.corFormBean.getListaCor().isEmpty()) {
					this.corFormBean.setListaCor(this.controllerCor.obterListaCor());
				}
				this.corFormBean.setCorManter(new CorVO());
				context.addMessage(null, new FacesMessage("Sucesso: Cor excluido com sucesso!"));
			}
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("Erro ao excluir o cor: " + e.getMessage()));
		}
	}
	
	// LISTENERS
	
	public void listenerIrInserirCor(ActionEvent evento) {
		
		this.corFormBean.setHead("Cadastrar Cor");
		this.corFormBean.setCorManter(new CorVO());
		this.corFormBean.setIsDisabled(false);
	}
	
	public void listenerIrExcluirCor(ActionEvent evento) {
		
		UIParameter comp = (UIParameter) evento.getComponent().findComponent("codigoCorExcluir");
		int codigo = Integer.valueOf((Integer)comp.getValue());
		
		if (codigo != 0) {
			CorVO corVO = new CorVO();
			corVO.setCodigo(codigo);
			this.corFormBean.setCorManter(this.controllerCor.obterCor(corVO));
		}
		this.corFormBean.setHead("Excluir Cor");
		this.corFormBean.setIsDisabled(true);
	}
	
	public void listenerIrEditarCor(ActionEvent evento) {
		
		UIParameter comp = (UIParameter) evento.getComponent().findComponent("codigoCorEditar");
		int codigo = Integer.valueOf((Integer)comp.getValue());
			
		if (codigo != 0) {
			CorVO corVO = new CorVO();
			corVO.setCodigo(codigo);
			this.corFormBean.setCorManter(this.controllerCor.obterCor(corVO));
		}
		this.corFormBean.setHead("Editar Cor");
		this.corFormBean.setIsDisabled(false);
	}
	
	public String listenerIrInseriHead(ActionEvent evento) {
		return this.corFormBean.getHead();
	}
}
